package com.sinodata.bsm.cicp.collector.cics;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class CICSChannel extends Collector {

    private static final Logger logger = Logger.getLogger(CICSChannel.class);

    @Override
    public String[] getPropMeta() {
        return new String[] { "status", "config" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "name" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] props = getPropMeta();
        String name = this.getParamMap().get("name");
        String shell1 = "cicsget -c rd -r " + name;
        String shell2 = "lssrc -a | grep cics";
        String regex = "(\\S+)\\s+\\d*\\s+(\\S+)";
        StringBuffer configBuffer = new StringBuffer();
        String statuString = "N/A";
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell1, 0, 0);
            String strLine = null;
            while ((strLine = reader.readLine()) != null) {
                if (strLine.indexOf("=") > -1) {
                    String temp = strLine.replaceAll("\"", "");
                    temp = temp.replaceAll("=", SeparatorConstants.COL_SEPARATOR);
                    configBuffer.append(temp).append(SeparatorConstants.LIN_SEPARATOR);
                }
            }
            reader = CommandReader.getReader(ip, shell2, 0, 0);
            Pattern pattern = Pattern.compile(regex);
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    if (strLine.contains(name)) {
                        statuString = matcher.group(2);
                        break;
                    }
                }
            }
            ResPropertyValue propertyValueX = new ResPropertyValue();
            values.add(propertyValueX);
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[props.length];
            propValues[0] = ResPropertyValue.createPropValue(statuString, props[0]);
            propValues[1] = ResPropertyValue.createPropValue(configBuffer.toString(), props[1]);
            return values;
        } catch (IOException e) {
            logger.error("collect CICS status and config status information error", e);
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close BufferedReader", e);
                }
            }

        }
    }

}
